package com.devshephard.spinLock;

import java.util.concurrent.atomic.AtomicInteger;

public class Test {

    public static void main(String[] args) throws InterruptedException {

        AtomicInteger atomicInteger = new AtomicInteger(1);
        System.out.println(atomicInteger.compareAndSet(1, 2) + "当前内存中的值：" + atomicInteger.get());
        System.out.println(atomicInteger.compareAndSet(2, 4) + "当前内存中的值：" + atomicInteger.get());

        MyLock myLock = new MyLock();

        new Thread(()->{
            try {
                myLock.getLock();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                myLock.unLock();
            }

        },"A").start();

        Thread.sleep(1000);

        new Thread(()->{
            try {
                myLock.getLock();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                myLock.unLock();
            }

        },"B").start();
    }

}
